package com.testing.redboy.fragment;


import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.testing.redboy.R;
import com.testing.redboy.bean.DbInfo;
import com.testing.redboy.bean.InfoBean;
import com.testing.redboy.db.InitDb;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class Info_Fragment extends Fragment implements View.OnClickListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private TextView back;
    private TextView into_shopCar_1;
    private ViewPager vp;
    private ImageView p1;
    private ImageView p2;
    private TextView gname;
    private TextView mprice;
    private TextView hy_price;
    private Button butt_color;
    private Button butt_size;
    private EditText count;
    private Button into_shopCar_2;
    private Button keep_it;
    private TextView kucun;
    private TextView pinglun;
    private ImageView[] ivs;
    private InfoBean.ProductBean product;
    private DbManager db;
    private ArrayList<DbInfo> alal;
    int mm = 1;


    public Info_Fragment() {
    }

    public static Info_Fragment newInstance(String param1, String param2) {
        Info_Fragment fragment = new Info_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        InitDb initDb = new InitDb();
        DbManager.DaoConfig config = initDb.init();
        db = x.getDb(config);

        alal = new ArrayList<>();


        View v = inflater.inflate(R.layout.fragment_info_, container, false);

        initView(v);

        ivs = new ImageView[]{p1, p2};

        setData();


        return v;
    }

    private void initView(View v) {
        back = (TextView) v.findViewById(R.id.limit_back);
        into_shopCar_1 = (TextView) v.findViewById(R.id.into_shopCar_1);
        vp = (ViewPager) v.findViewById(R.id.vp);
        p1 = (ImageView) v.findViewById(R.id.p1);
        p2 = (ImageView) v.findViewById(R.id.p2);
        gname = (TextView) v.findViewById(R.id.gname);
        mprice = (TextView) v.findViewById(R.id.mprice);
        hy_price = (TextView) v.findViewById(R.id.hy_price);
        butt_color = (Button) v.findViewById(R.id.butt_color);
        butt_size = (Button) v.findViewById(R.id.butt_size);
        count = (EditText) v.findViewById(R.id.count);
        into_shopCar_2 = (Button) v.findViewById(R.id.into_shopCar_2);
        keep_it = (Button) v.findViewById(R.id.keep_it);
        kucun = (TextView) v.findViewById(R.id.kucun);
        pinglun = (TextView) v.findViewById(R.id.pinglun);

        back.setOnClickListener(this);
        into_shopCar_1.setOnClickListener(this);
        into_shopCar_2.setOnClickListener(this);
        keep_it.setOnClickListener(this);
    }

    public void setData() {
        String u = "http://120.27.126.103:8080/ECServer_D/product";
        RequestParams params = new RequestParams(u);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Gson g = new Gson();
                InfoBean ib = g.fromJson(result, InfoBean.class);
                product = ib.product;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                final List<String> pic = product.pic;

                vp.setAdapter(new PagerAdapter() {
                    @Override
                    public int getCount() {
                        return pic.size();
                    }

                    @Override
                    public boolean isViewFromObject(View view, Object object) {
                        return view == object;
                    }

                    @Override
                    public Object instantiateItem(ViewGroup container, int position) {

                        ImageView iv = new ImageView(getActivity());
                        iv.setLayoutParams(new ViewGroup.LayoutParams(200, 150));
                        iv.setScaleType(ImageView.ScaleType.FIT_XY);
                        x.image().bind(iv, "http://120.27.126.103:8080/ECServer_D/" + pic.get(position));
                        container.addView(iv);

                        return iv;
                    }

                    @Override
                    public void destroyItem(ViewGroup container, int position, Object object) {
                        container.removeView((View) object);
                    }
                });

                vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        changePoint(position);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });

                vp.setCurrentItem(1);

                gname.setText(product.name);

                mprice.setText("￥" + product.price);
                mprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

                hy_price.setText("￥" + product.marketprice);

                count.setText(product.buyLimit + "");
            }
        });
    }

    public void changePoint(int index) {
        for (int i = 0; i < ivs.length; i++) {
            if (i == index) {
                ivs[i].setImageResource(R.drawable.slide_adv_selected);
            } else {
                ivs[i].setImageResource(R.drawable.slide_adv_normal);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.limit_back:
                getActivity().onBackPressed();
                break;
            case R.id.into_shopCar_1:

                String gname = product.name;
                int hy_p = product.marketprice;
                String im_u = "http://120.27.126.103:8080/ECServer_D/" + product.pic.get(0);


                String num = count.getText().toString();

                DbInfo dbInfo = new DbInfo(gname, im_u, num, hy_p, false);

                if (alal != null) {
                    alal.clear();
                }
                alal.add(dbInfo);

                try {
                    db.save(alal);
                    Log.d("zzz", db.findAll(DbInfo.class).toString());
                } catch (DbException e) {
                    e.printStackTrace();
                }

                //       Toast.makeText(getActivity(),"成功加入购物车1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.into_shopCar_2:
                Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                break;
            case R.id.keep_it:
                Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
